<template>
  <div class="update-gender">
    <van-picker
      show-toolbar
      :columns="columns"
      :default-index="defaultIndex"
      @cancel="$emit('close')"
      @confirm="onConfirm"
      @change="onGenderChange"
    >

    </van-picker>
  </div>
</template>

<script>
import { updateUserProfile } from '@/api/user'
export default {
  name: 'UpdateGender',
  components: {},

  props: {
    value: {
      type: Number,
      required: true
    }
  },

  data () {
    return {
      columns: ['男', '女'],
      defaultIndex: this.value
    }
  },

  computed: {},

  watch: {},

  created () {},

  mounted () {},

  methods: {
    onGenderChange (picker, value, index) {
      this.defaultIndex = index
      // console.log(picker, value, index)
    },
    async onConfirm () {
      this.$toast.loading({
        message: '保存中',
        forbidClick: true
      })
      try {
        // 请求提交更新用户性别
        await updateUserProfile({
          gender: this.defaultIndex
        })

        // 更新成功 -> 修改父组件的 name -> 关闭弹出层
        // 发布 input 事件，更新父组件 v-model 绑定的数据
        this.$emit('input', this.defaultIndex)
        this.$emit('close')
        this.$toast.success('保存成功')
      } catch (error) {
      }
      // console.log('onConfirm')
    }
  }
}

</script>
<style lang='less' scoped>
</style>
